/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components.DNDTree;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.dnd.DnDConstants;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author bfindeisen
 */
public class DNDTree extends JTree implements DNDTreeIFace {

    Insets autoscrollInsets = new Insets(20, 20, 20, 20); // insets

    public DNDTree(DefaultMutableTreeNode root) {
        setAutoscrolls(true);
        DefaultTreeModel treemodel = new DefaultTreeModel(root);
        setModel(treemodel);
        setRootVisible(true);
        setShowsRootHandles(false);//to show the root icon
        getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); //set single selection for the Tree
        setEditable(false);
        new DefaultTreeTransferHandler(this, DnDConstants.ACTION_COPY_OR_MOVE);
    }

    public void autoscroll(Point cursorLocation) {
        Insets insets = getAutoscrollInsets();
        Rectangle outer = getVisibleRect();
        Rectangle inner = new Rectangle(outer.x + insets.left, outer.y + insets.top, outer.width - (insets.left + insets.right), outer.height - (insets.top + insets.bottom));
        if (!inner.contains(cursorLocation)) {
            Rectangle scrollRect = new Rectangle(cursorLocation.x - insets.left, cursorLocation.y - insets.top, insets.left + insets.right, insets.top + insets.bottom);
            scrollRectToVisible(scrollRect);
        }
    }

    @Override
    public Insets getAutoscrollInsets() {
        return (autoscrollInsets);
    }

    @Override
    public DefaultMutableTreeNode makeDeepCopy(DefaultMutableTreeNode node) {
        DefaultMutableTreeNode copy = new DefaultMutableTreeNode(node.getUserObject());
        for (Enumeration e = node.children(); e.hasMoreElements();) {
            copy.add(makeDeepCopy((DefaultMutableTreeNode) e.nextElement()));
        }
        return (copy);
    }

    public static DefaultMutableTreeNode createTree() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("node1");
        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("node2");
        root.add(node1);
        root.add(node2);
        node1.add(new DefaultMutableTreeNode("sub1_1"));
        node1.add(new DefaultMutableTreeNode("sub1_2"));
        node1.add(new DefaultMutableTreeNode("sub1_3"));
        node2.add(new DefaultMutableTreeNode("sub2_1"));
        node2.add(new DefaultMutableTreeNode("sub2_2"));
        node2.add(new DefaultMutableTreeNode("sub2_3"));
        return (root);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            JFrame frame = new JFrame();
            Container contentPane = frame.getContentPane();
            contentPane.setLayout(new GridLayout(1, 2));
            DefaultMutableTreeNode root1 = DNDTree.createTree();
            DNDTree tree1 = new DNDTree(root1);
            //DefaultMutableTreeNode root2 = DNDTree.createTree();
            //DNDTree tree2 = new DNDTree(root2);
            contentPane.add(new JScrollPane(tree1));
            //contentPane.add(new JScrollPane(tree2));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.setVisible(true);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public JTree getTree() {
        return this;
    }
}
